package com.yenlien.traceyun.produce.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.produce.entity.ProcessSpecification;
import com.yenlien.traceyun.produce.entity.ProcessTechnologys;
import com.yenlien.traceyun.produce.service.TechnologysService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 工艺工序控制层
 */
@RestController
@RequestMapping("/technologys")
public class TechnologysController extends BaseController {

    @Autowired
    private TechnologysService technologysService;

    /**
     * 新增工艺工序
     *
     * @param processTechnology
     * @param request
     * @return
     */
    @PostMapping("/save")
    public Object save(ProcessTechnologys processTechnology, HttpServletRequest request) {
        List<ProcessSpecification> specification = processTechnology.getSpecification();
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        technologysService.save(processTechnology, specification, files);
        return success("success");
    }

    @PostMapping("/save2")
    public Object save2(@RequestBody ProcessTechnologys processTechnology) {
        List<ProcessSpecification> specification = processTechnology.getSpecification();
        technologysService.save2(processTechnology, specification);
        return success("success");
    }

    /**
     * 根据页面查询工艺工序
     *
     * @param params
     * @return
     */
    @PostMapping("/inquire")
    public Object inquire(@RequestBody JSONObject params) {
        Integer page = params.getInteger("page");
        if (page > 0) {
            Integer page1 = page - 1;
            return buildPage(technologysService.select(page1, UserAuthUtil.getLoginUser().getCompanyId()));
        } else {
            return fail("页面加载错误");
        }
    }

    @PostMapping("/findByListNoPage")
    public Object findByListNoPage(@RequestBody ProcessTechnologys processTechnologys) {
        if(StringUtils.isEmpty(processTechnologys.getCompanyId())){
            processTechnologys.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        return success(technologysService.findByListNoPage(processTechnologys));
    }

    /**
     * 搜索栏(模糊查询)
     *
     * @param params
     * @return
     */
    @PostMapping("/search")
    public Object search(@RequestBody JSONObject params) {
        String process = params.getString("processName");
        String processName = "%" + process + "%";
        if (processName.length() > 0) {
            return success(technologysService.search(processName));
        } else {
            return fail("信息未存在");
        }
    }



    /**
     * 根据id查询工艺工序
     *
     * @param params
     * @return
     */
    @PostMapping("/selectId")
    public Object selectId(@RequestBody JSONObject params) {
        String id = params.getString("id");
        if (id.length() > 0) {
            return success(technologysService.selectId(id));
        } else {
            return fail("物资不存在");
        }
    }

    /**
     * 根据id修改
     *
     * @param processTechnology specification
     */
    @PostMapping("/update")
    public Object update(ProcessTechnologys processTechnology, HttpServletRequest request) {
        List<ProcessSpecification> specification = processTechnology.getSpecification();
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("file"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("file");
        }
        technologysService.update(processTechnology, files);
        return success("success");
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/deletes/{id}")
    public Object deletes(@PathVariable String id) {
        technologysService.delete(id);
        return success("success");
    }

    /**
     * 导出工艺工序模板
     *
     * @param response
     */
    @RequestMapping("/efforts")
    public void efforts(HttpServletResponse response) {
        technologysService.efforts(response);
    }

    /**
     * 导入工艺工序
     *
     * @param request
     */
    @RequestMapping("/import")
    public Object imports(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String companyId = UserAuthUtil.getLoginUser().getCompanyId();
        MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
        technologysService.imports(file, userId, companyId);
        return success("导入成功");
    }


}